package com.example.newspapersmanageos.Controller;

import com.example.newspapersmanageos.Entity.Page;
import com.example.newspapersmanageos.Entity.ShowInfo;
import com.example.newspapersmanageos.Entity.User;
import com.example.newspapersmanageos.Service.LoginService;
import com.example.newspapersmanageos.Service.UploadService;
import com.sun.deploy.net.HttpResponse;
import com.sun.java.swing.plaf.windows.resources.windows;
import javafx.scene.control.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

@Controller
@RequestMapping("/loginContorller")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private UploadService uploadService;
//    @Value("${server.port}")
    private int port;
    @RequestMapping("/login")
    public String login(HttpServletRequest request,@RequestParam("acNumber") String acNumber, @RequestParam("userPwd") String userPwd,@RequestParam("identity") String identity) {
        User user = new User();
        user.setAcnumber(acNumber);
        user.setPwd(userPwd);
        user.setRole(identity);
        String result = loginService.login(user);
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
//        String msg = "此时访问的服务器是 " + address.getHostAddress()+ "/" + port;
        String s = request.getParameter("loginButton");
        if (result.equals("用户不存在")) {
            return "error";
        }
        if (identity.equals("user")&&s.equals("登录")){
            return "redirect:/loginContorller/userPage";
        }else if (identity.equals("admin")&&s.equals("登录")) {
            return "redirect:/loginContorller/homePage";
        }else if(s.equals("注册")){
            return "register";
        }

        return "error";
    }

    //跳转到用户页面
    @RequestMapping(value = "/userPage")
    public String userPage(HttpServletRequest request) {
        String page1 = request.getParameter("page");
        int pageNum = 1, pageSize = 1;
        InetAddress address = null;
        if (page1 != null && !page1.equals("")) {
            pageNum = Integer.parseInt(page1);
        }
        Page<ShowInfo> page = uploadService.getAllNewspaperInfo(pageNum, pageSize);
        request.setAttribute("page", page);
        return "userHome";
    }

    //注册操作
    @RequestMapping(value = "/insert")
    public void insert(HttpServletResponse response, HttpServletRequest request, @RequestParam("acNumber") String acNumber, @RequestParam("userName") String userName, @RequestParam("userPwd") String userPwd) throws IOException {
        User user = new User();
        user.setAcnumber(acNumber);
        user.setPwd(userPwd);
        user.setName(userName);
        String b = loginService.register(user);
        if (b.equals("注册成功")){
            response.getWriter().println("<script >window.alert('Register successfully!');window.location.href='/index.jsp'</script>");
        }else if(b.equals("用户名输入不合法")){
            response.getWriter().println("<script >window.alert('Illegal username input!');window.location.href='/register.jsp'</script>");
        }else if(b.equals("密码输入不合法")){
            response.getWriter().println("<script >window.alert('Illegal password input!');window.location.href='/register.jsp'</script>");
        }
    }
    //跳转到管理页面
    @RequestMapping(value = "/homePage")
    public String home(HttpServletRequest request) {
        String page1 = request.getParameter("page");
        int pageNum = 1, pageSize = 5;
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        if (page1 != null && !page1.equals("")) {
            pageNum = Integer.parseInt(page1);
        }
        Page<User> page = loginService.getAllUser(pageNum, pageSize);
        request.setAttribute("page", page);
        return "home";
    }

    //跳转到上传报刊页面
    @RequestMapping(value = "/uploadNewspaper")
    public String uploadNewspaper(HttpServletRequest request) {

        return "uploadNewspaper";
    }

    //跳转到报刊信息页面
    @RequestMapping(value = "/newspaperInfo")
    public String newspaperInfo(HttpServletRequest request) {

        return "newspaperInfo";
    }
}
